//
//  WBTabBar.m
//  CD1505WeiBo
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 LiChaoRan. All rights reserved.
//

#import "WBTabBar.h"
#import "WBTabBarButton.h"

@interface WBTabBar ()

//按钮数组
@property (nonatomic,strong)NSMutableArray *buttonArray;

//记录按钮
@property (nonatomic,strong)WBTabBarButton *selectBtn;

//添加中间按钮
@property (nonatomic, strong)UIButton *plusBtn;

@end

@implementation WBTabBar

- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        UIButton *plusBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:plusBtn];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        self.plusBtn=plusBtn;
        self,[plusBtn addTarget:self action:@selector(plusBtnPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

//懒加载
- (NSMutableArray *)buttonArray{
    if (_buttonArray==nil) {
        self.buttonArray=@[].mutableCopy;
    }
    return _buttonArray;
}

- (void)setTabBarItem:(UITabBarItem *)tabBarItem{
    //获取标题title，图片image
    _tabBarItem=tabBarItem;
    NSLog(@"%@",tabBarItem.title);
    
    //创建button
    WBTabBarButton *button=[WBTabBarButton buttonWithType:UIButtonTypeCustom];
    button.tabBarItem=tabBarItem;
    [self addSubview:button];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
    
    //存储按钮
    [self.buttonArray addObject:button];
    
    //第一个button
    if (self.buttonArray.count==1) {
        [self buttonPress:button];
    }
    
//    button.imageEdgeInsets=UIEdgeInsetsMake(0, 30, 20, 10);//设置button图片位置
//    button.titleEdgeInsets=UIEdgeInsetsMake(35, 0, 5, 10);//设置button位子位置
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //布局子视图
    
    //中间按钮
    CGFloat plusW=self.plusBtn.currentBackgroundImage.size.width;
    CGFloat plusH=self.plusBtn.currentBackgroundImage.size.height;
    self.plusBtn.frame=CGRectMake(0, 0, plusW, plusH);
    self.plusBtn.center=CGPointMake(CGRectGetWidth(self.frame)*0.5, CGRectGetHeight(self.frame)*0.5);
    
    //循环数组,并空出中间位置
    CGFloat btnW=CGRectGetWidth(self.frame)/((CGFloat)self.buttonArray.count+1);
    for (int i=0; i<self.buttonArray.count; i++) {
        WBTabBarButton *btn=self.buttonArray[i];
        CGFloat bX=btnW*i;
        if (i>1) {
            bX+=btnW;
        }
        CGFloat bY=0;
        CGFloat bW=btnW;
        CGFloat bH=CGRectGetHeight(self.frame);
        
        btn.frame=CGRectMake(bX, bY, bW, bH);
        
        [self addSubview:btn];
    }
}

- (void)buttonPress:(UIButton *)sender{
    //记录
    _selectBtn.selected=NO;
    sender.selected=YES;
    _selectBtn=sender;
    
    NSInteger index=[self.buttonArray indexOfObject:sender];
    
    //响应代理方法
//    if ([_delegate respondsToSelector:@selector(passIndex:)]) {
//        [_delegate passIndex:index];
//    }
    
    //block
    if (_passBlock) {
        _passBlock(index);
    }
}

- (void)plusBtnPress:(UIButton *)sender{
    if (_plutBtnPressedBlock) {
        _plutBtnPressedBlock();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
